package com.app.budgetbook.db;

import java.sql.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import com.app.budgetbook.model.Budgetbook;


public class dbAdapter {
	
	 // Database fields
    public static final String KEY_ROWID = "_id";
    public static final String KEY_Date = "date";
    public static final String KEY_Food = "food";
    public static final String KEY_Transport = "transport";
    public static final String KEY_accessories = "accessories";
    public static final String KEY_health = "health";
    public static final String KEY_family = "family";
    public static final String KEY_entertainment = "entertainment";
    private static final String DATABASE_TABLE = "category";
    private Context context;
    private SQLiteDatabase database;
    private dbHelper db;

    public dbAdapter(Context context) {

        this.context = context;
    }

    //opens the database via the TodoDatabaseHelper class
    public dbAdapter open() throws SQLException {

        db = new dbHelper(context);
        database = db.getWritableDatabase();
        return this;
    }

    public void close() {

        db.close();
    }

    /**
         * Create a new todo If the todo is successfully created return the new
         * rowId for that note, otherwise return a -1 to indicate failure.
         */

    public long createBudget(Budgetbook item) {

        ContentValues initialValues = createContentValues(item);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
         * Update the todo
         */

    public boolean updateBudget(long rowId, Budgetbook item) {

        ContentValues updateValues = createContentValues(item);

        return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Deletes todo
         */

    public boolean deletebudget(long rowId) {

        return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
         * Return a Cursor over the list of all todo in the database
         * 
         * @return Cursor over all notes
         */

    public Cursor fetchAllBudget() {

        return database.query(DATABASE_TABLE, new String[] {
                KEY_ROWID,KEY_Date, KEY_Food, KEY_Transport, KEY_health, KEY_entertainment, KEY_accessories, KEY_family
        }, null, null, null, null, null);
    }

    /**
         * Return a Cursor positioned at the defined todo
         */

    public Cursor fetchBudget(long rowid) throws SQLException {

        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID,KEY_Date, KEY_Food, KEY_Transport, KEY_health, KEY_entertainment, KEY_accessories, KEY_family
        }, KEY_ROWID + "=" + rowid, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public Cursor fetchBudget1(Date date) throws SQLException {

        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID,KEY_Date, KEY_Food, KEY_Transport, KEY_health, KEY_entertainment, KEY_accessories, KEY_family
        }, KEY_Date + "=" + date, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private ContentValues createContentValues(Budgetbook items) {

        ContentValues values = new ContentValues();
        values.put(KEY_Date, items.date);
        values.put(KEY_Food, items.food);
        values.put(KEY_Transport, items.transport);
        values.put(KEY_health, items.health);
        values.put(KEY_entertainment, items.entertainment);
        values.put(KEY_accessories, items.accessories);
        values.put(KEY_family, items.family);
        return values;
    }

}
